package com.catazy.module_project.di

import androidx.room.Room
import com.catazy.module_project.local.AppDatabase
import com.catazy.wanandroid.common_base.BaseApplication
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

/**
 * 作者: Tao
 * 日期: 2024/5/14
 * 时间: 15:20
 * 描述: 数据库模块 Hilt
 *      @InstallIn(SingletonComponent::class) 作用域为单例
 *      @InstallIn(ActivityComponent::class) 作用域为Activity
 *      @InstallIn(FragmentComponent::class) 作用域为Fragment
 *      @InstallIn(ViewModelComponent::class) 作用域为ViewModel
 *      @Module 作为一个模块
 */
@Module
@InstallIn(SingletonComponent::class)
class DataBaseModule {

    /**
     * 提供数据库
     */
    @Provides
    @Singleton
    fun provideDataBase(): AppDatabase {
        // 提供数据库
        return Room.databaseBuilder(
            BaseApplication.getContext().applicationContext,
            AppDatabase::class.java,
            "project.db"
        ).build()
    }

    /**
     * 提供项目树Dao
     */
    @Provides
    @Singleton
    fun provideProjectTreeDao(appDatabase: AppDatabase) = appDatabase.projectTreeDao()

    /**
     * 提供项目文章Dao
     */
    @Provides
    @Singleton
    fun provideArticleDetailDao(appDatabase: AppDatabase) = appDatabase.articleDetailDao()

    /**
     * 提供项目标签Dao
     */
    @Provides
    @Singleton
    fun provideTagDao(appDatabase: AppDatabase) = appDatabase.tagDao()
}